import tkinter as tk
from tkinter import *
from tkinter import messagebox

from data import DataUtils
from train import TrainUtils
from view import PredictView


class Start:
	def __init__(self, parent_window):
		parent_window.destroy()
		# 数据集
		self.data = None
		# 特征值
		self.X = None
		# 标签
		self.y = None
		# 是否经过缺失值处理
		self.is_pretreatment = FALSE
		# 是否提取特征值和标签
		self.is_features = FALSE
		# 是否训练了线性回归模型
		self.is_linear_model = FALSE
		# 是否训练了决策树模型
		self.is_tree_model = FALSE

		self.window = tk.Tk()
		self.window.title("主页")
		# 窗口大小
		self.window.geometry("308x400")
		self.id = StringVar()
		Button(self.window, text='数据预处理', width='30', command=self.pretreatment, font=("Verdana", 12)).grid(
			row=1, pady=3)
		Button(self.window, text='提取特征和标签', width='30', command=self.features, font=("Verdana", 12)).grid(
			row=2, pady=3)
		Button(self.window, text='训练线性回归模型', width='30', command=self.linear, font=("Verdana", 12)).grid(
			row=3, pady=3)
		Button(self.window, text='训练决策树模型', width='30', command=self.tree, font=("Verdana", 12)).grid(
			row=4, pady=3)
		Button(self.window, text='抽取数据集10%的数据预测', width='30', command=self.test, font=("Verdana", 12)).grid(
			row=5, pady=3)
		Button(self.window, text='随机数据预测', width='30', command=self.predict, font=("Verdana", 12)).grid(
			row=6, pady=3)
		self.window.mainloop()

	def pretreatment(self):
		if self.is_pretreatment:
			tk.messagebox.showwarning('提示！', message='数据已就绪！')
			return FALSE
		self.data = DataUtils.pretreatment()
		print(self.data.head())
		# 检查缺失值，数据类型
		check_lack = DataUtils.check_lack_data(self.data)
		check_type = DataUtils.check_type_data(self.data)
		if check_lack | check_type:
			if check_lack:
				ask_result = tk.messagebox.askokcancel('确认', '检测到数据缺失值，是否修补？')
				if ask_result:
					DataUtils.mend_lack_data(self.data)
					self.is_pretreatment = TRUE
			# 类型转换
			if check_type:
				ask_result = tk.messagebox.askokcancel('确认', '检测到数据含有字符串，是否替换为数字？')
				if ask_result:
					DataUtils.mend_type_data(self.data)
					self.is_pretreatment = TRUE
		else:
			self.is_pretreatment = TRUE
			tk.messagebox.showinfo('提示！', message='数据处理成功！')
		print(self.data.head())

	def features(self):
		if self.is_pretreatment:
			self.X, self.y = DataUtils.splitData(self.data)
			if self.X is not None and self.y is not None:
				self.is_features = TRUE
				tk.messagebox.showinfo('提示！', message='提取特征和标签成功！')
		else:
			tk.messagebox.showwarning('提示！', message='请先预处理数据！')

	def linear(self):
		if self.is_features:
			if self.is_linear_model:
				return
			TrainUtils.train_linear_model(self.X, self.y)
			self.is_linear_model = TRUE
			tk.messagebox.showinfo('提示！', message='线性回归模型准备就绪！')
		else:
			tk.messagebox.showwarning('警告！', message='请先预提取特征值和标签！')

	def tree(self):
		if self.is_features:
			if self.is_tree_model:
				return
			TrainUtils.train_tree_model(self.X, self.y)
			self.is_tree_model = TRUE
			# VisualUtils.plt_tree(TrainUtils.tr)
			tk.messagebox.showinfo('提示！', message='决策树模型准备就绪！')
		else:
			tk.messagebox.showwarning('警告！', message='请先预提取特征值和标签！')

	def test(self):
		if self.is_linear_model and self.is_tree_model:
			TrainUtils.default_predict(self.X, self.y)
		else:
			tk.messagebox.showwarning('警告！', message='请先训练模型！')

	def predict(self):
		if self.is_linear_model and self.is_tree_model:
			PredictView(self.window)
		else:
			tk.messagebox.showwarning('警告！', message='请先训练模型！')


if __name__ == '__main__':
	Start(tk.Tk())
